package com.iims.customer;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.iims.common.annotation.Log;
import com.iims.common.constant.UserConstants;
import com.iims.common.core.controller.BaseController;
import com.iims.common.core.domain.AjaxResult;
import com.iims.common.core.page.TableDataInfo;
import com.iims.common.enums.BusinessType;
import com.iims.customer.domain.CustomerType;
import com.iims.customer.service.ICustomerTypeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author fanpei
 * @date 2021/6/11 14:33
 */
@Api(tags = "E-客户类型管理")
@RestController
@RequestMapping("/business/customer/type")
public class CustomerTypeController extends BaseController {
    @Autowired
    private ICustomerTypeService cusTypeService;

    /**
     * 获取客户类型列表
     */
    @ApiOperation("分页查询客户类型列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前记录起始索引(从1开始)", name = "pageNum", paramType = "query", required = true),
            @ApiImplicitParam(value = "每页显示记录数（>0）", name = "pageSize", paramType = "query", required = true),
            @ApiImplicitParam(value = "排序列", name = "orderByColumn", paramType = "query"),
            @ApiImplicitParam(value = "升降序方式：0升序 1降序", name = "isAsc", paramType = "query"),
            @ApiImplicitParam(value = "客户类型名称", name = "cusTypeName", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('business:cusType:list')")
    @GetMapping("/list")
    public TableDataInfo list(@ApiIgnore CustomerType cusType) throws Exception {
        startPage();
        List<CustomerType> list = cusTypeService.selectCustomerTypeList(cusType);
        return getDataTable(list);
    }

    /**
     * 根据客户类型编号获取详细信息
     */
    @Deprecated
    //@ApiOperation("根据客户类型编号获取详细信息")
    //@ApiImplicitParam(name = "cusTypeId", value = "客户类型Id", paramType = "path", required = true)
    @PreAuthorize("@ss.hasPermi('business:cusType:query')")
    @GetMapping(value = "/{cusTypeId}")
    public AjaxResult getInfo(@PathVariable Long cusTypeId) {
        return AjaxResult.success(cusTypeService.selectOne(cusTypeId));
    }

    /**
     * 新增客户类型
     */
    @ApiOperation("新增客户类型")
    @ApiOperationSupport(ignoreParameters = {"cusTypeTd"})
    @PreAuthorize("@ss.hasPermi('business:cusType:add')")
    @Log(title = "客户类型管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CustomerType cusType) {
        if (UserConstants.NOT_UNIQUE.equals(cusTypeService.checkCustomerTypeNameUnique(cusType))) {
            return AjaxResult.error("新增客户类型'" + cusType.getCusTypeName() + "'失败，客户类型名称已存在");
        }
        return toAjax(cusTypeService.insertCustomerType(cusType));
    }

    /**
     * 修改客户类型
     */
    @ApiOperation("修改客户类型")
    @PreAuthorize("@ss.hasPermi('business:cusType:edit')")
    @Log(title = "客户类型管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CustomerType cusType) {
        if (UserConstants.NOT_UNIQUE.equals(cusTypeService.checkCustomerTypeNameUnique(cusType))) {
            return AjaxResult.error("修改客户类型'" + cusType.getCusTypeName() + "'失败，客户类型名称已存在");
        }
        return toAjax(cusTypeService.updateCustomerType(cusType));
    }

    /**
     * 删除客户类型
     */
    @ApiOperation("删除客户类型")
    @PreAuthorize("@ss.hasPermi('business:cusType:remove')")
    @Log(title = "客户类型管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cusTypeId}")
    public AjaxResult remove(@ApiParam("客户类型Id") @PathVariable Long cusTypeId) {
        if (cusTypeService.checkCustomerTypeUse(cusTypeId)) {
            return AjaxResult.error("删除客户类型失败，客户类型已使用！请先删除关联客户类型的客户！");
        }
        return toAjax(cusTypeService.deleteCustomerTypeById(cusTypeId));
    }
}
